<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ include file="/WEB-INF/component/commonTagLib.jsp"%>

<!DOCTYPE html>
<html>
<head>
    <title><spring:message code="message.lable.ShouHuoDiZhiBianJi"/></title>
    <%@ include file="/WEB-INF/component/commonCSS.jsp"%>
    <style type="text/css">
    	#isdefault {
    		margin-right: 5px;
    	}
    	span.required {
    		color: red;
    		margin-right: 6px;
    	}
    	.box-body{
    	margin-top:7px;
    	}
    </style>
</head>
<body class="skin-blue sidebar-mini fixed skin-blue-light-frame">
    <section class="content-header">
    <h1><spring:message code="${(opType==0)?'message.lable.ShouHuoDiZhiXinZeng':'message.lable.ShouHuoDiZhiBianJi'}"/></h1>
        <%-- <h1>${(opType==0)?"<spring:message code="message.lable.ShouHuoDiZhiXinZeng"/>":"<spring:message code="message.lable.ShouHuoDiZhiBianJi"/>"}</h1> --%>
        <ol class="breadcrumb">
            <li><a href="${ctx}/home.jsp"><i class="fa fa-home"></i><spring:message code="message.lable.first"/></a></li>
            <li><a href="#"><spring:message code="message.list.SheZhiPeiSongDiZhi"/></a></li>
            <li class="active"><a href="#"><spring:message code="${(opType==0)?'message.lable.ShouHuoDiZhiXinZeng':'message.lable.ShouHuoDiZhiBianJi'}"/></a></li>
        </ol>
    </section>
    <section class="content">
      <form id="form" class="form-horizontal" action="saveAddress.html" method="post">
        <div class=" box box-success ">
            
                <input type="hidden" name="recpAddrId" value="${address.recpAddrId}" />
                <input type="hidden" name="opType" value="${opType}" />
                <input type="hidden" id="userIds" name="userIds" value="${address.user.id}" />
                
                <div class="box-body">
                    
                    <div class="form-group form-group-xs">
                        <label for="user.name" class="col-sm-2 text-right"><span class="required">*</span><spring:message code="message.listForm.YongHuName"/><spring:message code="message.yanZheng.maoHao"/></label>
                        <div class='${user.main == 1 and opType == 0 ? "col-sm-3" : "col-sm-3"}'>
                             <input class="form-control" id="username" value="${address.user.name}" placeholder="<spring:message code="message.listForm.YongHuName"/>" type="text" readonly="readonly">
                        </div>
                        <c:if test="${user.main == 1 and opType == 0}">
                        	<button type="button" class="btn btn-success btn-sm" onclick="selectUserList();"><spring:message code="message.lable.selectSpacing"/></button>
                        </c:if>
                    </div>
                    
                    <div class="form-group form-group-xs">
                        <label for="recpAddrS" class="col-sm-2 text-right"><spring:message code="message.lable.SongHuoDiZhiJianCheng"/><spring:message code="message.yanZheng.maoHao"/></label>
                        <div class="col-sm-9">
                             <input class="form-control" id="recpAddrS" name="recpAddrS" value="${address.recpAddrS}" placeholder="<spring:message code="message.lable.SongHuoDiZhiJianCheng"/>" type="text">
                        </div>
                    </div>
                    
                     <div class="form-group form-group-xs">
                        <label for="recpAddr" class="col-sm-2 text-right"><span class="required">*</span><spring:message code="message.listForm.XiangXiSongHuoDiZhi"/><spring:message code="message.yanZheng.maoHao"/></label>
                        <div class="col-sm-9">
                            <input class="form-control" id="recpAddr" name="recpAddr" value="${address.recpAddr}" placeholder="<spring:message code="message.listForm.XiangXiSongHuoDiZhi"/>" type="text">
                        </div>
                    </div>
                    
                    <div class="form-group form-group-xs">
                        <label for="recpUser" class="col-sm-2 text-right"><span class="required">*</span><spring:message code="message.listForm.ShouHuoRen"/><spring:message code="message.yanZheng.maoHao"/></label>
                        <div class="col-sm-9">
                            <input class="form-control" id="recpUser" name="recpUser" value="${address.recpUser}" placeholder="<spring:message code="message.listForm.ShouHuoRen"/>" type="text">
                        </div>
                    </div>
                    
                    <div class="form-group form-group-xs">
                        <label for="recpUserphone" class="col-sm-2 text-right"><span class="required">*</span><spring:message code="message.lable.ShouHuoRenDianHua"/><spring:message code="message.yanZheng.maoHao"/></label>
                        <div class="col-sm-9">
                             <input class="form-control" id="recpUserphone" name="recpUserphone" value="${address.recpUserphone}" placeholder="<spring:message code="message.lable.ShouHuoRenDianHua"/>" type="text" maxlength="16">
                        </div>
                    </div>
                    
                    <div class="form-group form-group-xs">
                    	<div class="col-sm-2 text-right">
                        	
                        </div>
                        <div class="col-sm-9">
                        	<input id="isdefault" type="hidden" name="isdefault" type="checkbox" value="1" ${address.isdefault == 1 ? "checked='checked'" : ""} />
							<%-- <label for="isdefault"><spring:message code="message.button.SheWeiMoRenDiZhi"/></label> --%>
                        </div>
                    </div>
                    
                </div>
                
                <div class="box-footer text-center">
                    <button type="button" class="btn btn-success btn-sm" onclick="saveAddressed();"><spring:message code="message.button.saveSpacing"/></button>
                    <%-- <a href="toAddress.html" class="btn btn-danger btn-sm"><spring:message code="message.button.getBackSpacing"/></a> --%>
                </div>           
        </div>
      </form>  
    </section>
    
    <%@ include file="/WEB-INF/component/commonJS.jsp"%>
    <script type="text/javascript">
    
    	function selectUserList() {
    		var url = "${ctx}/address/toUserList.html?userIds=" + $("#userIds").val();
    		$.HN.dialog.open({
    			"id": "selectUserList", 
    			"title": "<spring:message code="message.list.YongHuXuanZeLieBiao"/>", 
    			"url": url, 
    			"data": {}, 
    			"width": 800, 
    			"height": 400, 
    			"closefunc":function(userList) {
    				if (userList.length > 0) {
        				var userIds = "";
        				var names = "";
        				for (var i=0; i<userList.length; i++) {
        					var user = userList[i];
        					userIds += user.id;
        					names += user.name;
        					if (i < userList.length - 1) {
        						userIds += ";";
        						names += "; ";
        					}
        				}
        				$("#userIds").val(userIds);
        				$("#username").val(names);
        			}
    			}
    		});
    	}
    
        function saveAddressed() {
            if (!validate()) {
                return;
            }
            $.HN.message.confirm("确定保存吗？", "<spring:message code="message.HN.alert.type"/>", "<spring:message code="message.button.confirm"/>").on(function(bool) {
        		if (bool) {
        			$("#form").ajaxSubmit({ 
                        dataType : "json",
                        timeout: 10000,
                        success : function(result, statusText) {
                            if (result.success) {
                                $.HN.message.alert("保存成功！", "<spring:message code="message.HN.alert.type"/>", "success", function() {
                                    document.location = "${ctx}/address/toAddressEdit.html";
                                });
                            } else {
                                $.HN.message.alert(result.msg || "保存失败！", "<spring:message code="message.HN.alert.type"/>", "error");
                            }
                        }
                    });
        		}
        	});
        }
        
        
        function validate() {
       	
        	var userIds = $.trim($("#userIds").val());
            if (userIds == null || userIds == "") {
                $.HN.message.alert("<spring:message code="message.yanZheng.QingXuanZeYongHu"/><spring:message code="message.yanZheng.ganTanHao"/>", "<spring:message code="message.HN.alert.type"/>", "warn");
                return false;
            }
            
            var recpAddrS = $.trim($("#recpAddrS").val());
            if(recpAddrS.length > 128){
                $.HN.message.alert('<spring:message code="message.yanZheng.SongHuoDiZhiJianChengTaiChang"/><spring:message code="message.yanZheng.ganTanHao"/><spring:message code="message.yanZheng.qingChongXinShuRu"/>', '<spring:message code="message.HN.alert.type"/>', 'warn');
                 return false;
            }

            var recpAddr = $.trim($("#recpAddr").val());
            if (recpAddr == null || recpAddr == "") {
                $.HN.message.alert("<spring:message code="message.yanZheng.QingShuRuXiangXiSongHuoDiZhi"/><spring:message code="message.yanZheng.ganTanHao"/>", "<spring:message code="message.HN.alert.type"/>", "warn");
                return false;
            } else if(recpAddr.length > 512){
                $.HN.message.alert('<spring:message code="message.yanZheng.XiangXiSongHuoDiZhiTaiChang"/><spring:message code="message.yanZheng.ganTanHao"/><spring:message code="message.yanZheng.qingChongXinShuRu"/>', '<spring:message code="message.HN.alert.type"/>', 'warn');
                 return false;
            }

            var recpUser = $.trim($("#recpUser").val());
            if (recpUser == null || recpUser == "") {
                $.HN.message.alert("<spring:message code="message.yanZheng.QingShuRuShouHuoRen"/><spring:message code="message.yanZheng.ganTanHao"/>", "<spring:message code="message.HN.alert.type"/>", "warn");
                return false;
            }else if(recpUser.length > 36){
                $.HN.message.alert('<spring:message code="message.yanZheng.SongHuoRenNameTaiChang"/><spring:message code="message.yanZheng.ganTanHao"/><spring:message code="message.yanZheng.qingChongXinShuRu"/>', '<spring:message code="message.HN.alert.type"/>', 'warn');
                return false;
           }

            var recpUserphone = $.trim($("#recpUserphone").val());
            var reg = new RegExp(regexEnum.phone);  
            if (recpUserphone == null || recpUserphone == "") {
                $.HN.message.alert("<spring:message code="message.yanZheng.QingShuRuShouHuoRenDianHua"/><spring:message code="message.yanZheng.ganTanHao"/>", "<spring:message code="message.HN.alert.type"/>", "warn");
                return false;
            }else if(!reg.test(recpUserphone)){
            	 $.HN.message.alert("<spring:message code="message.yanZheng.QingShuRuYouXiaoDianHua"/><spring:message code="message.yanZheng.ganTanHao"/>", "<spring:message code="message.HN.alert.type"/>", "warn");
                 return false;
            }

            return true;
        }
    
        
        
        
    </script>
    
</body>
</html>
